package com.fact.actions;

import java.util.ArrayList;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.fact.dto.ScenarioDTO;
import com.fact.dto.SegmentDTO;
import com.fact.models.ScenarioService;
import com.fact.utils.AppConstant;

public class Scenario extends BaseAction implements SessionAware{
	
	private static final long serialVersionUID = 1L;
	private Map<String,Object> session;
	private ArrayList<ScenarioDTO> scenarioList;
	
	private String page=AppConstant.SCENARIO;
	private String pageTitle="FMtool - Scenario List";

	ScenarioDTO scenario=new ScenarioDTO();
	private String opType;
	ScenarioService scenarioService=new ScenarioService();
	
	public String scenarioList()
	{
		if(opType==null || (!opType.equalsIgnoreCase("save") && !opType.equalsIgnoreCase("delete") && !opType.equalsIgnoreCase("edit")))
			this.session.put("operation_message", null);
		
		scenarioList= scenarioService.getScenarioList();
		
		return SUCCESS;
	}

	public String newScenarioForm()
	{		
		return SUCCESS;
	}
	public String saveScenario()
	{ 
		
		if(scenarioService.getScenario(scenario.getScenarioName())!=null)
			addActionMessage("Scenario Already Exist.");
		
		if(hasActionMessages())
			return  INPUT;


		if(scenarioService.saveScenario(scenario)==false)
				{
					addActionMessage("Error in Scenario Save Operation.");
					return INPUT;
				}
		else
				{
					this.session.put("operation_message", scenario.getScenarioName()+" has successfully been added in Scenario List.");
					return  SUCCESS;
				}

	}
	
   
	public String editScenario()
	{
		this.session.put("operation_message", null);
		ScenarioDTO scenarioTmp=scenarioService.getScenario(scenario.getScenarioId());
		boolean editFlag=false;
		if(scenarioTmp==null)
		{
			editFlag=true;
		}
		else
		{
			if(scenarioTmp.getScenarioId()!=scenario.getScenarioId())
			{
				editFlag=false;
			}
			else
			{
				editFlag=true;
			}
		}
		if(editFlag==true)
		{
			if(scenarioService.editScenario(scenario)==false)
			{
				addActionMessage("Error in Scenario Edit Operation.");
				return INPUT;
				
			}
			else
			{
				this.session.put("operation_message","Scenario with Scenario Name '"+ scenario.getScenarioName()+"' has successfully been updated.");
				return  SUCCESS;
			}
		}
		else
		{
			addActionMessage("Update Falied. "+scenario.getScenarioName()+" is already in the System.");
			return INPUT;
		}
	
	}
	public String deleteScenario()
	{
		if(scenarioService.deleteScenario(scenario.getScenarioId())==false)
		{
			addActionMessage("Error in Scenario Delete Operation.");
			return INPUT;
			
		}
		else
		{
			this.session.put("operation_message","Scenario with Scenario Name '"+ scenario.getScenarioName()+"' has successfully been deleted from Scenario List.");
			return  SUCCESS;
		}
	
	}
	
	
	public ArrayList<ScenarioDTO> getScenarioList() {
		return scenarioList;
	}

	public void setScenarioList(ArrayList<ScenarioDTO> scenarioList) {
		this.scenarioList = scenarioList;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getPageTitle() {
		return pageTitle;
	}

	public void setPageTitle(String pageTitle) {
		this.pageTitle = pageTitle;
	}

	public ScenarioDTO getScenario() {
		return scenario;
	}

	public void setScenario(ScenarioDTO scenario) {
		this.scenario = scenario;
	}

	public String getOpType() {
		return opType;
	}

	public void setOpType(String opType) {
		this.opType = opType;
	}

	public void setSession(Map<String, Object> session) 
	{
		this.session = session;
	}
	public ServletContext getServletContext()
	{
		return ServletActionContext.getServletContext();
	}
}
